package org.mclll.xwangplugin.cartography.recipes;

import de.tr7zw.nbtapi.NBT;
import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.TextComponent;
import net.kyori.adventure.text.format.NamedTextColor;
import net.kyori.adventure.text.format.TextDecoration;
import org.bukkit.Material;
import org.bukkit.enchantments.Enchantment;
import org.bukkit.inventory.Inventory;
import org.bukkit.inventory.ItemFlag;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.ItemMeta;

import java.util.Arrays;
import java.util.List;

/**
 * @author llB
 * @date 2024-06-15 16:41
 * @description todo
 */
public class CartographyTransferReelCopyRecipes extends CartographyCustomRecipes {

    /**
     * 判断是否是传送卷轴的正本
     * @param item -
     * @return -
     */
    @Override
    protected boolean isMainMaterial(ItemStack item) {
        if (item == null || item.getType() == Material.AIR) {
            return false;
        }
        List<Component> lore = item.lore();
        if (lore == null || lore.size() < 4) {
            return false;
        }
        String content = null;
        Component component = lore.get(3);
        List<Component> children = component.children();
        if (!children.isEmpty()) {
            content = ((TextComponent) children.get(0)).content();
        }
        return "一张传送卷轴的正本".equals(((TextComponent)lore.get(3)).content()) || (content != null && content.startsWith("一张传送卷轴的正本"));
    }

    /**
     * 判断是否是传送卷轴的拓本
     * @param item -
     * @return -
     */
    @Override
    protected boolean isResultItem(ItemStack item) {
        if (item == null || item.getType() == Material.AIR) {
            return false;
        }
        List<Component> lore = item.lore();
        if (lore == null || lore.size() < 3) {
            return false;
        }
        return "一张传送卷轴的复制品".equals(((TextComponent)item.lore().get(2)).content());
    }

    /**
     * 判断是否是空卷轴
     * @param item -
     * @return -
     */
    @Override
    protected boolean isViceMaterial(ItemStack item) {
        if (item == null || item.getType() == Material.AIR) {
            return false;
        }
        List<Component> lore = item.lore();
        if (lore == null || lore.size() < 3) {
            return false;
        }
        Component component = lore.get(2);
        if (component instanceof TextComponent) {
            String content = ((TextComponent) component).content();
            return content.startsWith("一张空白的卷轴");
        }
        return false;
    }

    /**
     * 生成传送卷轴拓本
     * @param cartographyInventory -
     * @return -
     */
    @Override
    protected ItemStack generalResultItem(Inventory cartographyInventory) {
        ItemStack result = new ItemStack(Material.PAPER);
        NBT.modify(result, nbt -> {
            nbt.setInteger("CustomModelData", 75026);
        });
        ItemStack transferReel = cartographyInventory.getItem(MAIN_SORT_INDEX);
        String worldName, pos;
        try {
            worldName = ((TextComponent) transferReel.lore().get(9).children().get(1)).content();
            pos = ((TextComponent) transferReel.lore().get(10).children().get(0)).content().substring(3);
        } catch (Exception e) {
            worldName = ((TextComponent)transferReel.lore().get(9).children().get(0)).content();
            pos = ((TextComponent)transferReel.lore().get(10)).content().substring(3);
        }
        ItemMeta itemMeta = result.getItemMeta();
        itemMeta.displayName(Component.text("传送卷轴·拓本").color(NamedTextColor.AQUA).decoration(TextDecoration.ITALIC, false));
        itemMeta.lore(Arrays.asList(
                        Component.text("消耗品").color(NamedTextColor.DARK_GRAY).decoration(TextDecoration.ITALIC, false),
                        Component.text("---------------------------------").color(NamedTextColor.WHITE).decoration(TextDecoration.ITALIC, false),
                        Component.text("一张传送卷轴的复制品").color(NamedTextColor.WHITE),
                        Component.text("使用后将传送至卷轴记载的地点").color(NamedTextColor.WHITE),
                        Component.text("---------------------------------").color(NamedTextColor.WHITE).decoration(TextDecoration.ITALIC, false),
                        Component.text("使用该卷轴的效果：").color(NamedTextColor.WHITE).decoration(TextDecoration.ITALIC, false),
                        Component.text("将传送到↓").color(NamedTextColor.GRAY).decoration(TextDecoration.ITALIC, false),
                        Component.text().content("世界：").color(NamedTextColor.DARK_GRAY)
                                .append(Component.text(worldName).color(NamedTextColor.YELLOW).decoration(TextDecoration.ITALIC, false))
                                .build(),
                        Component.text(String.format("坐标：%s", pos)).color(NamedTextColor.DARK_GRAY),
                        Component.text("---------------------------------").color(NamedTextColor.WHITE).decoration(TextDecoration.ITALIC, false),
                        Component.text("需下蹲时右键使用").color(NamedTextColor.GREEN).decoration(TextDecoration.ITALIC, false)
                )
        );
        itemMeta.addEnchant(Enchantment.DURABILITY, 1, true);
        result.setItemMeta(itemMeta);
        result.addItemFlags(ItemFlag.HIDE_ITEM_SPECIFICS, ItemFlag.HIDE_ENCHANTS);
        return result;
    }

    /**
     * 拓本合成的时候我们不清除正本，只清除空卷轴
     */
    @Override
    protected boolean clearMain() {
        return false;
    }

    @Override
    protected boolean clearVice() {
        return true;
    }
}
